﻿using System;
using System.Linq;
using System.Web.Mvc;
using System.Configuration;
using System.Web.Routing;

namespace Manazra.Helpers
{
    public static class CustomExtensions
    {
        public static string FormatUserName(this HtmlHelper helper, string userName, bool link)
        {
            string formattedUserName = userName;
            //get facebook seperator
            var fbIdent = ConfigurationManager.AppSettings["FBIdent"].ToString();
            String[] fbIdentArr = new String[]{fbIdent};
            
            //check for facebook seperator in username
            if (userName.IndexOf(fbIdent) != -1)
            {
                //split fb identifier from the user
                formattedUserName = userName.Split(fbIdentArr, StringSplitOptions.None).Last();
            }

            //return formatted string
            if(link)
                return String.Format("<a href='{0}'>{1}</a>", "/Authors/" + userName, formattedUserName);
            else
                return formattedUserName;
        }

        public static Route MapRouteWithName(this RouteCollection routes, string name, string url, object defaults, object constraints)
        {
            Route route = routes.MapRoute(name, url, defaults, constraints);
            route.DataTokens = new RouteValueDictionary();
            route.DataTokens.Add("RouteName", name);

            return route;
        }
    }
}
